package cn.itcast.zuoyebang.fragment.table2fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.zuoyebang.ExerciseActivity;
import cn.itcast.zuoyebang.R;
import cn.itcast.zuoyebang.bean.Exercise;

public class ExerciseAdapter extends BaseAdapter implements View.OnClickListener {

    private List<Exercise> exerciseList = new ArrayList<>();
    private Context mContext;

    public ExerciseAdapter(List<Exercise> list, Context context) {
        exerciseList = list;
        mContext = context;
    }

    @Override
    public int getCount() {
        return exerciseList == null ? 0 :exerciseList.size();
    }

    @Override
    public Object getItem(int i) {
        return exerciseList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        List<Object> holder = new ArrayList<>();
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.exercise_item,null);
            TextView idTV = view.findViewById(R.id.idTV);
            TextView titleTV = view.findViewById(R.id.titleTV);
            holder.add(idTV);
            holder.add(titleTV);
            holder.add(new Integer(i));
            view.setTag(holder);
            view.setOnClickListener(this);
        } else {
            holder = (List<Object>) view.getTag();
        }
        ((TextView)holder.get(0)).setText(String.valueOf(i+1));
        switch (exerciseList.get(i).color){
            case 0:
                ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_1);
                break;
            case 1:
                ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_2);
                break;
            case 2:
                ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_3);
                break;
            case 3:
                ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_4);
                break;
        }
        ((TextView)holder.get(1)).setText(exerciseList.get(i).title);
        return view;
    }


    @Override
    public void onClick(View view) {
        List<Object> holder = (List<Object>) view.getTag();
        int i = (Integer)holder.get(2);


        Intent intent = new Intent(mContext, ExerciseActivity.class);
        intent.putExtra("TITLE",exerciseList.get(i).title);
        intent.putExtra("URL",exerciseList.get(i).url);
        mContext.startActivity(intent);


    }
}
